<?php

namespace App\Http\Controllers;

use App\Models\Dian;
use App\Models\Role;
use App\Server\Excel;
use App\Server\Qny;
use App\Server\Validate;
use Illuminate\Http\Request;

class DianController extends Controller
{
    //列表
    public function list(){

        $data=Dian::paginate(10);
//        dd($data);
        return view('/dianList',['data'=>$data]);
    }
//表单
    public function dian(){
        return view('/dian');
    }
//表单提交
    public function add(Request $request){
        $param=$request->except('_token','s');
        //后端验证
        $validate=Validate::validate($param);

        $file=$param['img']->$request->store('file');
//        $qny=Qny::upload($param['img'],$file);
        $data=Dian::create($param);
        return redirect(url('list'));
    }
    //删除
    public function del($id){
        $data=Dian::destroy($id);
        $img=Dian::find($id);
        $imgs=$img['img'];
        $file=fopen("app/file/$imgs");
        unset($file);
        return redirect(url('list'));
    }

    //导出
    public function out(){
        // 设置表格的表头数据
        $header = ["A1" => "序号", "B1" => "充电桩编号", "C1" => "充电桩名称","D1"=>'终端类型','E1'=>'充电端口','F1'=>'当前状态','G1'=>'创建人'];
        //调Goods模型从goods表查询数据并转数组
        $data=Dian::all()->toArray();
        // 保存文件的类型
        $type= true;
        // 设置下载文件保存的名称
        $fileName = '充电桩表'.time();
        // 调用方法导出excel
        Excel::export($header,$type,$data,$fileName);
    }

    //导入
    public function enter(Request $request){
//        $files = $request->file("pic");
        $files=public_path('app/excel/');
        // 调用类库，读取excel中的内容
        $data = Excel::importExcel($files);
        dd($data);
    }
}
